home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / UNIXTOOL / GNU / PERL / PERL5.ZIP / !Perl / Lib / pl / perl5db < prev    next >
Text File  |  1995-06-28  |  15KB  |  565 lines

  1. package DB;
  2.  
  3. # modified Perl debugger, to be run from Emacs in perldb-mode
  4. # Ray Lischner (uunet!mntgfx!lisch) as of 5 Nov 1990
  5. # Johan Vromans -- upgrade to 4.0 pl 10
  6.  
  7. $header = '$RCSfile: perl5db.pl,v $$Revision: 4.1 $$Date: 92/08/07 18:24:07 $';
  8. #
  9. # This file is automatically included if you do perl -d.
  10. # It's probably not useful to include this yourself.
  11. #
  12. # Perl supplies the values for @line and %sub.  It effectively inserts
  13. # a &DB'DB(<linenum>); in front of every place that can
  14. # have a breakpoint.  It also inserts a do 'perldb.pl' before the first line.
  15. #
  16. # $Log:    perldb.pl,v $
  17.  
  18. local($^W) = 0;
  19.  
  20.  
  21. #$console = "/dev/tty";
  22.  
  23. #open(IN, "<$console") || 
  24. open(IN,  "<&STDIN");    # so we don't dingle stdin
  25. #open(OUT,">$console") || 
  26. open(OUT, ">&STDERR") ||
  27.     open(OUT, ">&STDOUT");    # so we don't dongle stdout
  28. select(OUT);
  29. $| = 1;                # for DB::OUT
  30. select(STDOUT);
  31. $| = 1;                # for real STDOUT
  32. $sub = '';
  33.  
  34. # Is Perl being run from Emacs?
  35. $emacs = $main::ARGV[0] eq '-emacs';
  36. shift(@main::ARGV) if $emacs;
  37.  
  38. $header =~ s/.Header: ([^,]+),v(\s+\S+\s+\S+).*$/$1$2/;
  39. print OUT "\nLoading DB routines from $header\n";
  40. print OUT ("Emacs support ",
  41.        $emacs ? "enabled" : "available",
  42.        ".\n");
  43. print OUT "\nEnter h for help.\n\n";
  44.  
  45. @ARGS;
  46.  
  47. sub DB {
  48.     &save;
  49.     ($pkg, $filename, $line) = caller;
  50.     $usercontext = '($@, $!, $,, $/, $\, $^W) = @saved;' .
  51.     "package $pkg;";        # this won't let them modify, alas
  52.     local(*dbline) = "::_<$filename";
  53.     $max = $#dbline;
  54.     if (($stop,$action) = split(/\0/,$dbline{$line})) {
  55.     if ($stop eq '1') {
  56.         $signal |= 1;
  57.     }
  58.     else {
  59.         $evalarg = "\$DB::signal |= do {$stop;}"; &eval;
  60.         $dbline{$line} =~ s/;9($|\0)/$1/;
  61.     }
  62.     }
  63.     if ($single || $trace || $signal) {
  64.     if ($emacs) {
  65.         print OUT "\032\032$filename:$line:0\n";
  66.     } else {
  67.         $prefix = $sub =~ /'|::/ ? "" : "${pkg}::";
  68.         $prefix .= "$sub($filename:";
  69.         if (length($prefix) > 30) {
  70.         print OUT "$prefix$line):\n$line:\t",$dbline[$line];
  71.         $prefix = "";
  72.         $infix = ":\t";
  73.         }
  74.         else {
  75.         $infix = "):\t";
  76.         print OUT "$prefix$line$infix",$dbline[$line];
  77.         }
  78.         for ($i = $line + 1; $i <= $max && $dbline[$i] == 0; ++$i) {
  79.         last if $dbline[$i] =~ /^\s*(}|#|\n)/;
  80.         print OUT "$prefix$i$infix",$dbline[$i];
  81.         }
  82.     }
  83.     }
  84.     $evalarg = $action, &eval if $action;
  85.     if ($single || $signal) {
  86.     $evalarg = $pre, &eval if $pre;
  87.     print OUT $#stack . " levels deep in subroutine calls!\n"
  88.         if $single & 4;
  89.     $start = $line;
  90.       CMD:
  91.     while ((print OUT "  DB<", $#hist+1, "> "), $cmd=&gets) {
  92.         {
  93.         $single = 0;
  94.         $signal = 0;
  95.         $cmd eq '' && exit 0;
  96.         chop($cmd);
  97.         $cmd =~ s/\\$// && do {
  98.             print OUT "  cont: ";
  99.             $cmd .= &gets;
  100.             redo CMD;
  101.         };
  102.         $cmd =~ /^q$/ && exit 0;
  103.         $cmd =~ /^$/ && ($cmd = $laststep);
  104.         push(@hist,$cmd) if length($cmd) > 1;
  105.         ($i) = split(/\s+/,$cmd);
  106.         eval "\$cmd =~ $alias{$i}", print OUT $@ if $alias{$i};
  107.         $cmd =~ /^h$/ && do {
  108.             print OUT "
  109. T        Stack trace.
  110. s        Single step.
  111. n        Next, steps over subroutine calls.
  112. r        Return from current subroutine.
  113. c [line]    Continue; optionally inserts a one-time-only breakpoint 
  114.         at the specified line.
  115. <CR>        Repeat last n or s.
  116. l min+incr    List incr+1 lines starting at min.
  117. l min-max    List lines.
  118. l line        List line;
  119. l        List next window.
  120. -        List previous window.
  121. w line        List window around line.
  122. l subname    List subroutine.
  123. f filename    Switch to filename.
  124. /pattern/    Search forwards for pattern; final / is optional.
  125. ?pattern?    Search backwards for pattern.
  126. L        List breakpoints and actions.
  127. S        List subroutine names.
  128. t        Toggle trace mode.
  129. b [line] [condition]
  130.         Set breakpoint; line defaults to the current execution line; 
  131.         condition breaks if it evaluates to true, defaults to \'1\'.
  132. b subname [condition]
  133.         Set breakpoint at first line of subroutine.
  134. d [line]    Delete breakpoint.
  135. D        Delete all breakpoints.
  136. a [line] command
  137.         Set an action to be done before the line is executed.
  138.         Sequence is: check for breakpoint, print line if necessary,
  139.         do action, prompt user if breakpoint or step, evaluate line.
  140. A        Delete all actions.
  141. V [pkg [vars]]    List some (default all) variables in package (default current).
  142. X [vars]    Same as \"V currentpackage [vars]\".
  143. < command    Define command before prompt.
  144. > command    Define command after prompt.
  145. ! number    Redo command (default previous command).
  146. ! -number    Redo number\'th to last command.
  147. H -number    Display last number commands (default all).
  148. q or ^D        Quit.
  149. p expr        Same as \"print DB::OUT expr\" in current package.
  150. = [alias value]    Define a command alias, or list current aliases.
  151. command        Execute as a perl statement in current package.
  152.  
  153. ";
  154.             next CMD; };
  155.         $cmd =~ /^t$/ && do {
  156.             $trace = !$trace;
  157.             print OUT "Trace = ".($trace?"on":"off")."\n";
  158.             next CMD; };
  159.         $cmd =~ /^S$/ && do {
  160.             foreach $subname (sort(keys %sub)) {
  161.             print OUT $subname,"\n";
  162.             }
  163.             next CMD; };
  164.         $cmd =~ s/^X\b/V $pkg/;
  165.         $cmd =~ /^V$/ && do {
  166.             $cmd = "V $pkg"; };
  167.         $cmd =~ /^V\b\s*(\S+)\s*(.*)/ && do {
  168.             local ($savout) = select(OUT);
  169.             $packname = $1;
  170.             @vars = split(' ',$2);
  171.             do 'dumpvar.pl' unless defined &main::dumpvar;
  172.             if (defined &main::dumpvar) {
  173.             &main::dumpvar($packname,@vars);
  174.             }
  175.             else {
  176.             print DB::OUT "dumpvar.pl not available.\n";
  177.             }
  178.             select ($savout);
  179.             next CMD; };
  180.         $cmd =~ /^f\b\s*(.*)/ && do {
  181.             $file = $1;
  182.             if (!$file) {
  183.             print OUT "The old f command is now the r command.\n";
  184.             print OUT "The new f command switches filenames.\n";
  185.             next CMD;
  186.             }
  187.             if (!defined $main::{'_<' . $file}) {
  188.             if (($try) = grep(m#^_<.*$file#, keys %main::)) {
  189.                 $file = substr($try,2);
  190.                 print "\n$file:\n";
  191.             }
  192.             }
  193.             if (!defined $main::{'_<' . $file}) {
  194.             print OUT "There's no code here anything matching $file.\n";
  195.             next CMD;
  196.             }
  197.             elsif ($file ne $filename) {
  198.             *dbline = "::_<$file";
  199.             $max = $#dbline;
  200.             $filename = $file;
  201.             $start = 1;
  202.             $cmd = "l";
  203.             } };
  204.         $cmd =~ /^l\b\s*([':A-Za-z_][':\w]*)/ && do {
  205.             $subname = $1;
  206.             $subname = "main::" . $subname unless $subname =~ /'|::/;
  207.             $subname = "main" . $subname if substr($subname,0,1)eq "'";
  208.             $subname = "main" . $subname if substr($subname,0,2)eq "::";
  209.             ($file,$subrange) = split(/:/,$sub{$subname});
  210.             if ($file ne $filename) {
  211.             *dbline = "::_<$file";
  212.             $max = $#dbline;
  213.             $filename = $file;
  214.             }
  215.             if ($subrange) {
  216.             if (eval($subrange) < -$window) {
  217.                 $subrange =~ s/-.*/+/;
  218.             }
  219.             $cmd = "l $subrange";
  220.             } else {
  221.             print OUT "Subroutine $1 not found.\n";
  222.             next CMD;
  223.             } };
  224.         $cmd =~ /^w\b\s*(\d*)$/ && do {
  225.             $incr = $window - 1;
  226.             $start = $1 if $1;
  227.             $start -= $preview;
  228.             $cmd = 'l ' . $start . '-' . ($start + $incr); };
  229.         $cmd =~ /^-$/ && do {
  230.             $incr = $window - 1;
  231.             $cmd = 'l ' . ($start-$window*2) . '+'; };
  232.         $cmd =~ /^l$/ && do {
  233.             $incr = $window - 1;
  234.             $cmd = 'l ' . $start . '-' . ($start + $incr); };
  235.         $cmd =~ /^l\b\s*(\d*)\+(\d*)$/ && do {
  236.             $start = $1 if $1;
  237.             $incr = $2;
  238.             $incr = $window - 1 unless $incr;
  239.             $cmd = 'l ' . $start . '-' . ($start + $incr); };
  240.         $cmd =~ /^l\b\s*(([\d\$\.]+)([-,]([\d\$\.]+))?)?/ && do {
  241.             $end = (!$2) ? $max : ($4 ? $4 : $2);
  242.             $end = $max if $end > $max;
  243.             $i = $2;
  244.             $i = $line if $i eq '.';
  245.             $i = 1 if $i < 1;
  246.             if ($emacs) {
  247.             print OUT "\032\032$filename:$i:0\n";
  248.             $i = $end;
  249.             } else {
  250.             for (; $i <= $end; $i++) {
  251.                 print OUT "$i:\t", $dbline[$i];
  252.                 last if $signal;
  253.             }
  254.             }
  255.             $start = $i;    # remember in case they want more
  256.             $start = $max if $start > $max;
  257.             next CMD; };
  258.         $cmd =~ /^D$/ && do {
  259.             print OUT "Deleting all breakpoints...\n";
  260.             for ($i = 1; $i <= $max ; $i++) {
  261.             if (defined $dbline{$i}) {
  262.                 $dbline{$i} =~ s/^[^\0]+//;
  263.                 if ($dbline{$i} =~ s/^\0?$//) {
  264.                 delete $dbline{$i};
  265.                 }
  266.             }
  267.             }
  268.             next CMD; };
  269.         $cmd =~ /^L$/ && do {
  270.             for ($i = 1; $i <= $max; $i++) {
  271.             if (defined $dbline{$i}) {
  272.                 print OUT "$i:\t", $dbline[$i];
  273.                 ($stop,$action) = split(/\0/, $dbline{$i});
  274.                 print OUT "  break if (", $stop, ")\n" 
  275.                 if $stop;
  276.                 print OUT "  action:  ", $action, "\n" 
  277.                 if $action;
  278.                 last if $signal;
  279.             }
  280.             }
  281.             next CMD; };
  282.         $cmd =~ /^b\b\s*([':A-Za-z_][':\w]*)\s*(.*)/ && do {
  283.             $subname = $1;
  284.             $cond = $2 || '1';
  285.             $subname = "${pkg}::" . $subname
  286.             unless $subname =~ /'|::/;
  287.             $subname = "main" . $subname if substr($subname,0,1) eq "'";
  288.             $subname = "main" . $subname if substr($subname,0,2) eq "::";
  289.             ($filename,$i) = split(/:/, $sub{$subname});
  290.             $i += 0;
  291.             if ($i) {
  292.             *dbline = "::_<$filename";
  293.             ++$i while $dbline[$i] == 0 && $i < $#dbline;
  294.             $dbline{$i} =~ s/^[^\0]*/$cond/;
  295.             } else {
  296.             print OUT "Subroutine $subname not found.\n";
  297.             }
  298.             next CMD; };
  299.         $cmd =~ /^b\b\s*(\d*)\s*(.*)/ && do {
  300.             $i = ($1?$1:$line);
  301.             $cond = $2 || '1';
  302.             if ($dbline[$i] == 0) {
  303.             print OUT "Line $i not breakable.\n";
  304.             } else {
  305.             $dbline{$i} =~ s/^[^\0]*/$cond/;
  306.             }
  307.             next CMD; };
  308.         $cmd =~ /^d\b\s*(\d+)?/ && do {
  309.             $i = ($1?$1:$line);
  310.             $dbline{$i} =~ s/^[^\0]*//;
  311.             delete $dbline{$i} if $dbline{$i} eq '';
  312.             next CMD; };
  313.         $cmd =~ /^A$/ && do {
  314.             for ($i = 1; $i <= $max ; $i++) {
  315.             if (defined $dbline{$i}) {
  316.                 $dbline{$i} =~ s/\0[^\0]*//;
  317.                 delete $dbline{$i} if $dbline{$i} eq '';
  318.             }
  319.             }
  320.             next CMD; };
  321.         $cmd =~ /^<\s*(.*)/ && do {
  322.             $pre = action($1);
  323.             next CMD; };
  324.         $cmd =~ /^>\s*(.*)/ && do {
  325.             $post = action($1);
  326.             next CMD; };
  327.         $cmd =~ /^a\b\s*(\d+)(\s+(.*))?/ && do {
  328.             $i = $1;
  329.             if ($dbline[$i] == 0) {
  330.             print OUT "Line $i may not have an action.\n";
  331.             } else {
  332.             $dbline{$i} =~ s/\0[^\0]*//;
  333.             $dbline{$i} .= "\0" . action($3);
  334.             }
  335.             next CMD; };
  336.         $cmd =~ /^n$/ && do {
  337.             $single = 2;
  338.             $laststep = $cmd;
  339.             last CMD; };
  340.         $cmd =~ /^s$/ && do {
  341.             $single = 1;
  342.             $laststep = $cmd;
  343.             last CMD; };
  344.         $cmd =~ /^c\b\s*(\d*)\s*$/ && do {
  345.             $i = $1;
  346.             if ($i) {
  347.             if ($dbline[$i] == 0) {
  348.                 print OUT "Line $i not breakable.\n";
  349.                 next CMD;
  350.             }
  351.             $dbline{$i} =~ s/(\0|$)/;9$1/;    # add one-time-only b.p.
  352.             }
  353.             for ($i=0; $i <= $#stack; ) {
  354.             $stack[$i++] &= ~1;
  355.             }
  356.             last CMD; };
  357.         $cmd =~ /^r$/ && do {
  358.             $stack[$#stack] |= 2;
  359.             last CMD; };
  360.         $cmd =~ /^T$/ && do {
  361.             local($p,$f,$l,$s,$h,$a,@a,@sub);
  362.             for ($i = 1; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
  363.             @a = ();
  364.             for $arg (@args) {
  365.                 $_ = "$arg";
  366.                 s/'/\\'/g;
  367.                 s/([^\0]*)/'$1'/
  368.                 unless /^(?: -?[\d.]+ | \*[\w:]* )$/x;
  369.                 s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
  370.                 s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
  371.                 push(@a, $_);
  372.             }
  373.             $w = $w ? '@ = ' : '$ = ';
  374.             $a = $h ? '(' . join(', ', @a) . ')' : '';
  375.             push(@sub, "$w$s$a from file $f line $l\n");
  376.             last if $signal;
  377.             }
  378.             for ($i=0; $i <= $#sub; $i++) {
  379.             last if $signal;
  380.             print OUT $sub[$i];
  381.             }
  382.             next CMD; };
  383.         $cmd =~ /^\/(.*)$/ && do {
  384.             $inpat = $1;
  385.             $inpat =~ s:([^\\])/$:$1:;
  386.             if ($inpat ne "") {
  387.             eval '$inpat =~ m'."\a$inpat\a";    
  388.             if ($@ ne "") {
  389.                 print OUT "$@";
  390.                 next CMD;
  391.             }
  392.             $pat = $inpat;
  393.             }
  394.             $end = $start;
  395.             eval '
  396.             for (;;) {
  397.             ++$start;
  398.             $start = 1 if ($start > $max);
  399.             last if ($start == $end);
  400.             if ($dbline[$start] =~ m'."\a$pat\a".'i) {
  401.                 if ($emacs) {
  402.                 print OUT "\032\032$filename:$start:0\n";
  403.                 } else {
  404.                 print OUT "$start:\t", $dbline[$start], "\n";
  405.                 }
  406.                 last;
  407.             }
  408.             } ';
  409.             print OUT "/$pat/: not found\n" if ($start == $end);
  410.             next CMD; };
  411.         $cmd =~ /^\?(.*)$/ && do {
  412.             $inpat = $1;
  413.             $inpat =~ s:([^\\])\?$:$1:;
  414.             if ($inpat ne "") {
  415.             eval '$inpat =~ m'."\a$inpat\a";    
  416.             if ($@ ne "") {
  417.                 print OUT "$@";
  418.                 next CMD;
  419.             }
  420.             $pat = $inpat;
  421.             }
  422.             $end = $start;
  423.             eval '
  424.             for (;;) {
  425.             --$start;
  426.             $start = $max if ($start <= 0);
  427.             last if ($start == $end);
  428.             if ($dbline[$start] =~ m'."\a$pat\a".'i) {
  429.                 if ($emacs) {
  430.                 print OUT "\032\032$filename:$start:0\n";
  431.                 } else {
  432.                 print OUT "$start:\t", $dbline[$start], "\n";
  433.                 }
  434.                 last;
  435.             }
  436.             } ';
  437.             print OUT "?$pat?: not found\n" if ($start == $end);
  438.             next CMD; };
  439.         $cmd =~ /^!+\s*(-)?(\d+)?$/ && do {
  440.             pop(@hist) if length($cmd) > 1;
  441.             $i = ($1?($#hist-($2?$2:1)):($2?$2:$#hist));
  442.             $cmd = $hist[$i] . "\n";
  443.             print OUT $cmd;
  444.             redo CMD; };
  445.         $cmd =~ /^!(.+)$/ && do {
  446.             $pat = "^$1";
  447.             pop(@hist) if length($cmd) > 1;
  448.             for ($i = $#hist; $i; --$i) {
  449.             last if $hist[$i] =~ $pat;
  450.             }
  451.             if (!$i) {
  452.             print OUT "No such command!\n\n";
  453.             next CMD;
  454.             }
  455.             $cmd = $hist[$i] . "\n";
  456.             print OUT $cmd;
  457.             redo CMD; };
  458.         $cmd =~ /^H\b\s*(-(\d+))?/ && do {
  459.             $end = $2?($#hist-$2):0;
  460.             $hist = 0 if $hist < 0;
  461.             for ($i=$#hist; $i>$end; $i--) {
  462.             print OUT "$i: ",$hist[$i],"\n"
  463.                 unless $hist[$i] =~ /^.?$/;
  464.             };
  465.             next CMD; };
  466.         $cmd =~ s/^p( .*)?$/print DB::OUT$1/;
  467.         $cmd =~ /^=/ && do {
  468.             if (local($k,$v) = ($cmd =~ /^=\s*(\S+)\s+(.*)/)) {
  469.             $alias{$k}="s~$k~$v~";
  470.             print OUT "$k = $v\n";
  471.             } elsif ($cmd =~ /^=\s*$/) {
  472.             foreach $k (sort keys(%alias)) {
  473.                 if (($v = $alias{$k}) =~ s~^s\~$k\~(.*)\~$~$1~) {
  474.                 print OUT "$k = $v\n";
  475.                 } else {
  476.                 print OUT "$k\t$alias{$k}\n";
  477.                 };
  478.             };
  479.             };
  480.             next CMD; };
  481.         }
  482.         $evalarg = $cmd; &eval;
  483.         print OUT "\n";
  484.     }
  485.     if ($post) {
  486.         $evalarg = $post; &eval;
  487.     }
  488.     }
  489.     ($@, $!, $,, $/, $\, $^W) = @saved;
  490.     ();
  491. }
  492.  
  493. sub save {
  494.     @saved = ($@, $!, $,, $/, $\, $^W);
  495.     $, = ""; $/ = "\n"; $\ = ""; $^W = 0;
  496. }
  497.  
  498. # The following takes its argument via $evalarg to preserve current @_
  499.  
  500. sub eval {
  501.     eval "$usercontext $evalarg; &DB::save";
  502.     print OUT $@;
  503. }
  504.  
  505. sub action {
  506.     local($action) = @_;
  507.     while ($action =~ s/\\$//) {
  508.     print OUT "+ ";
  509.     $action .= &gets;
  510.     }
  511.     $action;
  512. }
  513.  
  514. sub gets {
  515.     local($.);
  516.     <IN>;
  517. }
  518.  
  519. sub catch {
  520.     $signal = 1;
  521. }
  522.  
  523. sub sub {
  524.     push(@stack, $single);
  525.     $single &= 1;
  526.     $single |= 4 if $#stack == $deep;
  527.     if (wantarray) {
  528.     @i = &$sub;
  529.     $single |= pop(@stack);
  530.     @i;
  531.     }
  532.     else {
  533.     $i = &$sub;
  534.     $single |= pop(@stack);
  535.     $i;
  536.     }
  537. }
  538.  
  539. $trace = $signal = $single = 0;    # uninitialized warning suppression
  540.  
  541. @hist = ('?');
  542. $SIG{'INT'} = "DB::catch";
  543. $deep = 100;        # warning if stack gets this deep
  544. $window = 10;
  545. $preview = 3;
  546.  
  547. @stack = (0);
  548. @ARGS = @ARGV;
  549. for (@args) {
  550.     s/'/\\'/g;
  551.     s/(.*)/'$1'/ unless /^-?[\d.]+$/;
  552. }
  553.  
  554. if (-f $rcfile) {
  555.     do "./$rcfile";
  556. }
  557. elsif (-f "$ENV{'LOGDIR'}/$rcfile") {
  558.     do "$ENV{'LOGDIR'}/$rcfile";
  559. }
  560. elsif (-f "$ENV{'HOME'}/$rcfile") {
  561.     do "$ENV{'HOME'}/$rcfile";
  562. }
  563.  
  564. 1;
  565.